local filelog = require "filelog"

require "zrttzenum"

local json = require "cjson"
json.encode_sparse_array(true,1,1)

-------------------------

local CardTool = { }

local CardColor = {
	Dot = 0,	--筒子
	White = 1,	--白板
}

--特殊奖励定义
local SpecialPrize = {
	SPECIAL_PRIZE_DANPAI = 0,       --单牌
	SPECIAL_PRIZE_TIANGANG = 1,     --天杠
	SPECIAL_PRIZE_DUIZI = 2,        --对子
	SPECIAL_PRIZE_ZHIZUN = 3,       --至尊
}

--图标id
local Picture_id = {
	1101,    --闲家1赢
	1102,    --闲家2赢
	1103,    --闲家3赢
	1104,    --庄家通杀
}

-----------------------------------

--
--获得牌型：至尊、对子、天杠、单牌
--0单张 1天杠 2对子 3至尊
--
function CardTool.get_cards_type(cards)
	if #cards ~= 2 then
		return
	end

	local cards_type = SpecialPrize.SPECIAL_PRIZE_DANPAI
	local points = (cards[1] + cards[2]) % 10
	
	--天杠（二八杠）
	if (cards[1] == 2 and cards[2] == 8) or (cards[1] == 8 and cards[2] == 2) then
		cards_type = SpecialPrize.SPECIAL_PRIZE_TIANGANG
	elseif cards[1] == cards[2] then
		if cards[1] == 10 then
			cards_type = SpecialPrize.SPECIAL_PRIZE_ZHIZUN
		else
			cards_type = SpecialPrize.SPECIAL_PRIZE_DUIZI
		end
	end

	--获得最大牌，白板为最大牌
	local max_card = cards[1]
	local min_card = cards[2]
	if cards[1] < cards[2] then
		max_card = cards[2]
		min_card = cards[1]
	end

	local result = {
		cards_type = cards_type,
		max_card = max_card,
		min_card = min_card,
		points = points,
	}

	return result
end

--比较玩家牌
--true 庄赢 false 闲赢
function CardTool.compare_cards(banker_cards, player_cards)
	if not banker_cards or not player_cards then
		return
	end

	local banker_win = false
	local player_win = true
	if banker_cards.cards_type ~= player_cards.cards_type then   --比较牌型 至尊（2白板）> 对子（豹子） > 天杠（二八） > 单牌
		return (banker_cards.cards_type < player_cards.cards_type)
	elseif banker_cards.points ~= player_cards.points then       --牌型相同，比较点数
		if banker_cards.cards_type == SpecialPrize.SPECIAL_PRIZE_DUIZI then
			return (banker_cards.max_card < player_cards.max_card)
		else
			return (banker_cards.points < player_cards.points)
		end
	elseif banker_cards.max_card ~= player_cards.max_card then   --点数相同，比较单牌大小
		return (banker_cards.max_card < player_cards.max_card)
	elseif banker_cards.min_card ~= player_cards.min_card then   --点数相同，比较单牌大小
		return (banker_cards.min_card < player_cards.min_card)
	else
		return banker_win
	end
end

--转换牌数据
function CardTool.convert_cards(cards)
	local all_cards = {
		[1] = {},
		[2] = {},
		[3] = {},
		[4] = {},
	}
	local dice_nums = {}

	local is_flag = false	
	for _,carditem in ipairs(cards) do
		if carditem.status == "open" then
			local ct = CardColor[carditem.suit] or 0
			local cv = math.floor(tonumber(carditem.point))
			local card = ct * 10 + cv
			local ty = string.sub(carditem.type, 1, 1)
			if ty == "p" then
				local i = tonumber(string.sub(carditem.type, -1, -1))
				table.insert(all_cards[i+1], card)
			elseif ty == "b" then
				table.insert(all_cards[1], card)
			elseif ty == "d" then
				table.insert(dice_nums, card)
			end
			is_flag = true
		end
	end
	
	if not is_flag then
		return {}
	end
	
	return all_cards,dice_nums
end

--获得开牌结果
function CardTool.get_opencardresult(gameData)
	
	--转换牌数据
	local all_cards = CardTool.convert_cards(gameData.cardInfo)
	if #all_cards == 0 then
		return false
	end
	
	local all_results = {}
	for i,cards in pairs(all_cards) do
	   all_results[i] = CardTool.get_cards_type(cards)
	end
	
	local result_list = {
		all_cards = all_cards,
		all_results = all_results,
	}
	
	return result_list
end

--获得摇骰子结果
function CardTool.get_dicesresult(gameData)
	--转换牌数据
	local _,dice_nums = CardTool.convert_cards(gameData.doorCards)
	
	return dice_nums
end

--获得游戏结果
function CardTool.get_gameresult(gameResult)
	local ext_prize_list = {}
	
	--转换牌数据
	local all_cards = CardTool.convert_cards(gameResult.cards)
	
	local all_results = {}
	for i,cards in pairs(all_cards) do
	   all_results[i] = CardTool.get_cards_type(cards)
	end
	
	--判定开奖结果，第一副牌为庄家牌，其他3副为闲家
	--[[
	local winners = json.decode(gameResult.winner)
	if winners.player1 == "player" then
		table.insert(ext_prize_list, ETTZBetBtnId.BET_BTN_ID_PLAYER_ONE)
	end
	if winners.player2 == "player" then
		table.insert(ext_prize_list, ETTZBetBtnId.BET_BTN_ID_PLAYER_TWO)
	end
	if winners.player3 == "player" then
		table.insert(ext_prize_list, ETTZBetBtnId.BET_BTN_ID_PLAYER_THREE)
	end
	--]]
	
	--判定开奖结果，第一副牌为庄家牌，其他3副为闲家
	for i = 2,4 do
		local is_win = CardTool.compare_cards(all_results[1],all_results[i])
		all_results[i].is_win = is_win
		if is_win then
			table.insert(ext_prize_list, Picture_id[i-1])
		end
	end

	--庄通杀
	if #ext_prize_list == 0 then
		table.insert(ext_prize_list, ETTZExtraPrizeType.EXTRA_PRIZE_TYPE_KILLALL)
	end
	
	local result_list = {
		all_cards = all_cards,
		all_results = all_results,
		prize_id = 0,
		ext_prize_list = ext_prize_list,
	}
	
	return result_list
end

return CardTool
